<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Like
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: IndexController.php 2010-09-07 16:05 idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Like
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Like_IndexController extends Core_Controller_Action_Standard
{
	protected $_object_id;
	protected $_object;
	
	public function init() 
	{
		if ($this->_getParam('format') != 'json'){
			return false;
		}
    
    $viewer = Engine_Api::_()->user()->getViewer();

    if (!Engine_Api::_()->core()->hasSubject()){
      Engine_Api::_()->core()->setSubject($viewer);
    }
		
		$this->view->object = $this->_object = $this->_getParam('object');
		$this->view->object_id = $this->_object_id = $this->_getParam('object_id');	
	}
	
	public function indexAction()
	{
    if (!Engine_Api::_()->getApi('settings', 'core')->getSetting('like.profile.enabled', 1)){
      $this->_redirectCustom(array('route' => 'home'));
      return ;
    }
    
    if (!Engine_Api::_()->authorization()->isAllowed('like', null, 'create')){
      $this->_redirectCustom(array('route' => 'home'));
      return ;
    }
    
		$this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer();
		
	  if (!$viewer->getIdentity()){
      $this->_redirectCustom(array('route' => 'home'));
      return ;
    }
    
		$items = Engine_Api::_()->like()->getLikeClubsByLike($viewer);
		$this->view->navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('like_profile');
		
		$this->view->pages = $items['pages'];
		if ($this->view->pages) $this->view->pages->setItemCountPerPage($this->view->pages->getTotalItemCount());

		$this->view->users = $items['users'];
		if ($this->view->users) $this->view->users->setItemCountPerPage($this->view->users->getTotalItemCount());
	}
	
	public function likeAction() 
	{
	  if (!Engine_Api::_()->getApi('settings', 'core')->getSetting('like.profile.enabled', 1)){
      $this->view->error = 1;
      $this->view->html = Zend_Registry::get('Zend_Translate')->_('like_LIKE_ADMIN_TURN_OFF_ERROR');
      return ;
    }
    
    if (!Engine_Api::_()->authorization()->isAllowed('like', null, 'create')){
      $this->view->error = 2;
      $this->view->html = Zend_Registry::get('Zend_Translate')->_('like_LIKE_ADMIN_TURN_OFF_ERROR');
    }
    
		$object = Engine_Api::_()->getItem($this->_object, $this->_object_id);
		$viewer = Engine_Api::_()->user()->getViewer();

    if (!$viewer->getIdentity()){
      $this->view->error =  3;
      $this->view->html =  Zend_Registry::get('Zend_Translate')->_('like_LIKE_LOGIN_ERROR');
    }

		$this->view->link = 'unlike';
		if (Engine_Api::_()->like()->like($object)){
			$activityApi = Engine_Api::_()->getDbtable('actions', 'activity');
      $action = $activityApi->addActivity($viewer, $object, 'like');
      $activityApi->attachActivity($action, $object);
		}else{
			$this->view->error = 1;
			$this->view->html = 'Error';
		}
	}
	
	public function unlikeAction() 
	{
    if (!Engine_Api::_()->getApi('settings', 'core')->getSetting('like.profile.enabled', 1)){
      $this->view->error = 1;
      $this->view->html = Zend_Registry::get('Zend_Translate')->_('like_LIKE_ADMIN_TURN_OFF_ERROR');
      return ;
    }
    
    if (!Engine_Api::_()->authorization()->isAllowed('like', null, 'create')){
      $this->view->error = 2;
      $this->view->html = Zend_Registry::get('Zend_Translate')->_('like_LIKE_ADMIN_TURN_OFF_ERROR');
    }
    
		$object = Engine_Api::_()->getItem($this->_object, $this->_object_id);
    $viewer = Engine_Api::_()->user()->getViewer();
    
    if (!$viewer->getIdentity()){
      $this->view->error =  3;
      $this->view->html =  Zend_Registry::get('Zend_Translate')->_('like_LIKE_LOGIN_ERROR');
    }

		$this->view->link = 'like';
		if (!Engine_Api::_()->like()->unlike($object)){
			$this->view->error = 1;
			$this->view->html = Zend_Registry::get('Zend_Translate')->_('like_LIKE_UNDEFINED_ERROR');
		}
	}

  public function seeLikedAction()
  {
    $user_id = (int)$this->_getParam('user_id');
    if (!$user_id){
      $this->view->error = Zend_Registry::get('Zend_Translate')->_('like_LIKE_UNDEFINED_ERROR');
      return ;
    }

    $this->view->subject = $subject = Engine_Api::_()->getItem('user', $user_id);
    if (!$subject->getIdentity()){
      $this->view->error = Zend_Registry::get('Zend_Translate')->_('like_This like club was deleted.');
      return ;
    }
    
    $this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer();
    if (!$viewer->getIdentity()){
      $this->view->error = Zend_Registry::get('Zend_Translate')->_('like_LIKE_LOGIN_ERROR');
      return ;
    }

    $list = $this->_getParam('list', 'all');
    $moduleApi = Engine_Api::_()->getDbTable('modules', 'core');
    $table = Engine_Api::_()->getDbTable('clubs', 'like');
    
    $params = array('like_id' => $subject->getIdentity());

    if ($list == 'mutual'){
      $params['mutual'] = $viewer->getIdentity();
    }

    $users = $table->getLikeClubObjectsByType($params, 'user');

    if (!empty($users)){
      $users->setItemCountPerPage($users->getTotalItemCount());
      $this->view->users = $users;
    }

    if ($moduleApi->isModuleEnabled('page')){
      $pages = $table->getLikeClubObjectsByType($params, 'page');
      if (!empty($pages)){
        $pages->setItemCountPerPage($pages->getTotalItemCount());
        $this->view->pages = $pages; 
      }
    }

    if ($this->_getParam('format') == 'json'){
      $this->view->html = $this->view->render('_composeItems.tpl');
    }
  }
}